package com.blame.onlinemusicserver.controller;


import com.blame.onlinemusicserver.model.Music;
import com.blame.onlinemusicserver.service.MusicService;
import com.blame.onlinemusicserver.tools.ResponseBodyMessage;
import jakarta.servlet.http.HttpServletRequest;
import jakarta.servlet.http.HttpServletResponse;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.ResponseEntity;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.multipart.MultipartFile;
import java.util.List;


@Slf4j
@RestController
@RequestMapping("/music")
public class MusicController {



    @Autowired
    private MusicService musicService;

    @RequestMapping("/upload")
    public ResponseBodyMessage<Boolean> insertMusic(@RequestParam String singer,
                                                    @RequestParam("filename") MultipartFile file,
                                                    HttpServletRequest request,
                                                    HttpServletResponse response){
        return  musicService.insertMusic(singer, file, request, response);

    }

    @GetMapping("/get")
    public ResponseEntity<byte[]> get(@RequestParam String path){
        return musicService.get(path);
    }


    @RequestMapping("/delete")
    public ResponseBodyMessage<Boolean> deleteMusicById(@RequestParam Integer id){
        return musicService.deleteMusicById(id);
    }


    @RequestMapping("/deleteMany")
    public ResponseBodyMessage<Boolean> deleteMusicByIdMany(@RequestParam("id") List<Integer> ids){
        return musicService.deleteMusicByIdMany(ids);
    }

    @RequestMapping("/findmusic")
    public ResponseBodyMessage<List<Music>> findMusic(@RequestParam(required = false) String musicName){
        return  musicService.findMusic(musicName);
    }

}
